﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data.Linq;
using BandAid.DalCE;

namespace BandAid
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
            
        }

      


        private void Grid_LostFocus(object sender, RoutedEventArgs e)
        {
            ProcessLogic.Instance.UpdateDal();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
       { 

            Archives.SongArchive.AddRange(from x in ProcessLogic.Instance.dc.SONGS orderby x.ARTIST_ID select x);
            Archives.ArtistArchive.AddRange(from x in ProcessLogic.Instance.dc.ARTISTS orderby x.NAME select x);
            Archives.ShowArchive.AddRange(from x in ProcessLogic.Instance.dc.SHOWS orderby x.DATE select x);
            Archives.PlaceArchive.AddRange(from x in ProcessLogic.Instance.dc.PLACES orderby x.NAME select x);
            Archives.ShowSongArchive = new List<SHOW_SONGS>();


        }

       
        private void New_Artist(object sender, RoutedEventArgs e)
        {
            ARTISTS a = new ARTISTS();
            a.NAME = "New Artist";
            a.DEFAULT_SIAE_OWNER = "Rights Owner"; 

            ProcessLogic.Instance.dc.ARTISTS.InsertOnSubmit(a);
            ProcessLogic.Instance.dc.SubmitChanges();
            Archives.ArtistArchive.Add(a);
                      
            ArtistsList.Items.Refresh();
        }

        private void Delete_Artist(object sender, RoutedEventArgs e)
        {
            ARTISTS victim = ArtistsList.SelectedItem as ARTISTS;

            if (victim != null)
            {
                ProcessLogic.Instance.dc.ARTISTS.DeleteOnSubmit((ARTISTS)ArtistsList.SelectedItem);
                ProcessLogic.Instance.dc.SubmitChanges();
                Archives.ArtistArchive.Remove(victim);
                ArtistsList.Items.Refresh();
            }
        }

        private void Delete_Song(object sender, RoutedEventArgs e)
        {
           SONGS victim = ArtistsList.SelectedItem as SONGS;

            if (victim != null)
            {
                ProcessLogic.Instance.dc.SONGS.DeleteOnSubmit(victim);
                ProcessLogic.Instance.dc.SubmitChanges();
                Archives.SongArchive.Remove(victim);
                SongList.Items.Refresh();
            }
        }

        private void New_Song(object sender, RoutedEventArgs e)
        {
            SONGS a = new SONGS();

            a.TITLE = "New Song";
            a.LENGTH = 0;
            a.ARTIST_ID = Archives.ArtistArchive[0].ID;
            
      

            ProcessLogic.Instance.dc.SONGS.InsertOnSubmit(a);
            ProcessLogic.Instance.dc.SubmitChanges();
            Archives.SongArchive.Add(a);

            //Archives.SongArchive.Sort();
            SongList.Items.Refresh();
            
        }

        //private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        //{
        //    Archives.ShowSongArchive.Clear();
        //    ShowSongsList.ItemsSource = null;

        //    SHOW selected = ShowList.SelectedItem as SHOW;
        //    if (selected != null)
        //        Archives.ShowSongArchive.AddRange(selected.SHOW_SONGs);
          
        //    ShowSongsList.ItemsSource = Archives.ShowSongArchive;
            
        //}

   

        private void AddToCurrentShow(object sender, RoutedEventArgs e)
        {
            SHOWS CurrentShow = (SHOWS) ShowList.Items.CurrentItem;

            SONGS CurrentSong = (SONGS)SongList.Items.CurrentItem;

            if (CurrentShow != null && CurrentSong != null)
            {
                SHOW_SONGS newPair = new SHOW_SONGS();
                newPair.SHOW_ID = CurrentShow.ID;
                newPair.SONG_ID = CurrentSong.ID;

                ProcessLogic.Instance.dc.SHOW_SONGS.InsertOnSubmit(newPair);
                ProcessLogic.Instance.UpdateDal();
                ShowSongsList.Items.Add(newPair);


            }
        }

        private void DeleteSongFromShow(object sender, RoutedEventArgs e)
        {
            SHOW_SONGS curShowSong = (SHOW_SONGS) ShowSongsList.Items.CurrentItem;

            if (curShowSong != null)
            {
                ProcessLogic.Instance.dc.SHOW_SONGS.DeleteOnSubmit(curShowSong);
                ProcessLogic.Instance.UpdateDal();
            //    ShowSongsList.ItemsSource.Remove(curShowSong);
            }
        }


      

       
    }
}
